//
//  MultiSeColleCell.swift
//  ZYA
//
//  Created by 邱仙凯 on 2025/5/19.
//  Copyright © 2025 zwb. All rights reserved.
//

// 快照型
class MultiSeImgCell: UICollectionViewCell {
    
  // 图片按钮
    var btn: UIButton = {
        let v  = UIButton()
        v.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        v.backgroundColor = UIColor(kshex: "eeeeee")
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
        v.setTitleColor(Theme.primaryColor(), for: .normal)
          return v
    }()
    // 灰色遮罩
    private var cover:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex: "bbbbbb")
        v.layer.cornerRadius = 5

        v.alpha = 0.6
        return v
    }()
    var deleteBtn:UIButton!

    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    func initUtil() {
        self.contentView.backgroundColor = .clear
          self.contentView.addSubview(btn)
        self.contentView.addSubview(cover)
        btn.snp.makeConstraints { (make) in
             make.left.right.equalTo(self.contentView)
            make.top.bottom.equalTo(self.contentView)
        }
        cover.snp.makeConstraints { (make) in
             make.left.right.equalTo(self.contentView)
            make.top.bottom.equalTo(self.contentView)
        }
        
        deleteBtn = UIButton(frame: CGRect(x:(self.ks_width - 25),y:0,width:25,height:25))
        deleteBtn.setBackgroundImage(UIImage(named:"ksphoto_delete"), for: .normal)
        self.addSubview(deleteBtn)
        
    }
}
// 文本型
class MultiSeTextCell: UICollectionViewCell {
    
  
    var btn: UIButton = {
        let v  = UIButton()
        v.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        v.backgroundColor = UIColor(kshex: "#CCE5FF")
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
         v.setTitleColor(UIColor(kshex: "#4972F9"), for: .normal)
          return v
    }()
  
     

    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    func initUtil() {
        self.contentView.backgroundColor = .clear
          self.contentView.addSubview(btn)
         btn.snp.makeConstraints { (make) in
             make.left.right.equalTo(self.contentView)
            make.top.bottom.equalTo(self.contentView)
        }
      
        
   
        
    }
}

//获得cell快照
func captureThumbnail(of cell: UITableViewCell) -> UIImage? {
    // 确保布局已更新
    cell.layoutIfNeeded()
    
    // 生成整个 cell 的快照
    let renderer = UIGraphicsImageRenderer(size: cell.bounds.size)
    let fullImage = renderer.image { _ in
        cell.drawHierarchy(in: cell.bounds, afterScreenUpdates: true)
    }
    
    // 获取图像的缩放比例
    let scale = fullImage.scale
    // 定义需要裁剪的区域（左上角 100x100 点）
    let cropRect = CGRect(x: 50, y: 15, width: 180, height: 180)
    
    // 转换为实际像素区域
    let scaledCropRect = CGRect(
        x: cropRect.origin.x * scale,
        y: cropRect.origin.y * scale,
        width: cropRect.width * scale,
        height: cropRect.height * scale
    )
    
    // 裁剪图像
    guard let cgImage = fullImage.cgImage?.cropping(to: scaledCropRect) else {
        return nil
    }
    
    // 生成最终缩略图（保留原缩放比例）
    return UIImage(cgImage: cgImage, scale: scale, orientation: .up)
}
